package com.platform.study.thread.threadpool;

/**
 * Created with IntelliJ IDEA.
 * User: wangyanginfo
 * Date: 13-1-5
 * Time: 下午4:02
 * To change this template use File | Settings | File Templates.
 */
public class TaskThread extends Thread {

    private BlockingQueue taskQueue = null;
    private boolean isStopped = false;

    public TaskThread(BlockingQueue queue) {
        taskQueue = queue;
    }

    @Override
    public void run() {
        System.out.println("执行任务线程！！！！" + taskQueue);
        while (!isStopped()) {
            try {
                Runnable runnable = (Runnable) taskQueue.dequeue();
                runnable.run();
            } catch (Exception e) {
                //log or otherwise report exception,
                //but keep pool thread alive.
            }
        }
    }

    @Override
    public synchronized void interrupt() {
        isStopped = true;
        super.interrupt();//break pool thread out of dequeue() call.
    }

    public synchronized boolean isStopped() {
        return isStopped;
    }

}
